package com.qf.purchasing.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.common.base.exception.ServiceException;
import com.qf.common.base.result.RespResult;
import com.qf.common.base.result.ResultCode;
import com.qf.purchasing.entity.Apply;
import com.qf.purchasing.mapper.ApplyMapper;
import com.qf.purchasing.qo.ApplyQO;
import com.qf.purchasing.service.ApplyService;
import com.qf.purchasing.vo.ApplyVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ApplyServiceImpl implements ApplyService {
    //flag[0] :增加修改标志  1：添加申请操作，2：添加已存在的申请操作，更新数量和价格，3：用户删除操作
    //flag[1] :增加或修改的参数   ids,applyQO.getApplyId(),applyQO.getNum()
    //public static int[] flag = new int[2];
    public static List<Object> flag = new ArrayList<>();

    @Resource
    private ApplyMapper applyMapper;

    @Override
    public RespResult<PageInfo<ApplyVO>> query(int page, int size, String title, String startTime, String endTime) {
        PageInfo<ApplyVO> pageInfo = PageHelper.startPage(page, size).doSelectPageInfo(
                () -> applyMapper.selectList(title, startTime, endTime)
        );
        if (pageInfo.getTotal() == 0) {
            throw new ServiceException(ResultCode.QUERY_ERROR);
        }
        return RespResult.success(ResultCode.SUCCESS, pageInfo);
    }

    @Override
    public RespResult<String> addApplyService(ApplyQO applyQO) {
        //查询此条申请是否存在，根据id判断
        Apply ApplyResult = applyMapper.selectById(applyQO.getApplyId());
        if (ObjectUtils.isEmpty(ApplyResult)) {
            //不存在，插入
            Apply apply = new Apply();
            BeanUtils.copyProperties(applyQO, apply);
            apply.setMoney(applyQO.getPrice() * applyQO.getNum());
            int count = applyMapper.insert(apply);
            if (count < 1) {
                throw new ServiceException(ResultCode.INSERT_ERROR);
            }

            flag.add(0, 1);
            flag.add(1, applyQO.getApplyId());
            return RespResult.success("添加成功！");
        } else {
            int count = applyMapper.updateNumById(applyQO.getApplyId(), applyQO.getNum());
            if (count < 1) {
                throw new ServiceException(ResultCode.INSERT_ERROR);
            }

            flag.add(0, 2);
            flag.add(1, applyQO);
            return RespResult.success("添加成功,数量更新成功！");
        }
    }

    @Override
    public RespResult<String> deleteApplyByIds(int[] applyIds) {
        int count = applyMapper.updateStatusById(applyIds);
        /*if (count < 1) {
            throw new ServiceException(ResultCode.DELETE_ERROR);
        }
        return RespResult.success("记录删除成功！");*/

        if (count == applyIds.length) {
            flag.add(0, 3);
            flag.add(1, applyIds);
            return RespResult.success("记录删除成功！");
        } else if (count < 1) {
            throw new ServiceException(ResultCode.DELETE_ERROR);
        } else {
            return RespResult.success("数据有误，部分删除成功！");
        }
    }

    @Override
    public RespResult<String> modify(ApplyQO applyQO) {
        Apply apply = new Apply();
        BeanUtils.copyProperties(applyQO, apply);
        apply.setMoney(applyQO.getPrice() * applyQO.getNum());
        int count = applyMapper.updateById(apply);
        if (count < 1) {
            throw new ServiceException(ResultCode.UPDATE_ERROR);
        }
        return RespResult.success("记录更新成功！");
    }

    @Override
    public RespResult<ApplyVO> selectApplyById(int applyId) {
        ApplyVO applyVO = applyMapper.selectApplyVOById(applyId);
        if (!ObjectUtils.isEmpty(applyVO)) {
            return RespResult.success(ResultCode.SUCCESS, applyVO);
        } else {
            throw new ServiceException(ResultCode.QUERY_ERROR);
        }
    }

    @Override
    public RespResult<String> recallSchedule() {
        if ((int) flag.get(0) == 1) {
            //上一次操作是新增，则删除新增数据
            int count = applyMapper.updateById((int) flag.get(1));
            if (count < 0) {
                throw new ServiceException(ResultCode.RECALL_ERROR);
            }
            return RespResult.success("撤回新增成功！");
        } else if ((int) flag.get(0) == 2) {
            //上一次操作是新增已有，增加了数量，则减少数量
            ApplyQO applyQO = (ApplyQO) flag.get(1);
            int count = applyMapper.updateNumTwoById(applyQO.getApplyId(), applyQO.getNum());
            if (count < 0) {
                throw new ServiceException(ResultCode.RECALL_ERROR);
            }
            return RespResult.success("撤回新增数量成功！");
        } else {
            //上一次操作是删除
            int count = applyMapper.updateByIds((int[]) flag.get(1));
            if (count < 0) {
                throw new ServiceException(ResultCode.RECALL_ERROR);
            }
            return RespResult.success("撤回删除成功！");
        }
    }
}
